﻿// The MIT License(MIT)
//
// Copyright(c) 2021 Alberto Rodriguez Orozco & LiveCharts Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Text;
using LiveChartsGenerators.Definitions;
using LiveChartsGenerators.Frameworks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace LiveChartsGenerators.Templates;

public static class UIPropertyTempaltes
{
    public static string GetTemplate(XamlProperty target, FrameworkTemplate template)
    {
        var propertyName = target.Name;

        var type = target.DeclaringType;

        var displayFormat = new SymbolDisplayFormat(
            typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameOnly,
            genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);

        var declaringType = type.ToDisplayString(displayFormat);

        var displayFormat2 = new SymbolDisplayFormat(
            typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
            genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);

        var docs = target.XmlDocs ?? $@"
    /// <summary>
    ///    Gets or sets the <see cref=""{propertyName}""/> property.
    /// </summary>
";

        var propertyType = $"global::{target.Type.ToDisplayString(displayFormat2)}";

        var sanitizedPropertyType = target.Type.IsReferenceType && propertyType.EndsWith("?")
            ? propertyType.Substring(0, propertyType.Length - 1)
            : propertyType;

        var converter = string.Empty;
        if (XamlObjectTempaltes.TypeConverters.TryGetValue(target.Type.ToDisplayString(), out var typeConverter))
            converter = @$"[System.ComponentModel.TypeConverter(typeof({typeConverter}))]
    ";

        var isXaml = template.Key is "Avalonia" or "WinUI" or "Maui" or "WPF";

        return @$"// <auto-generated>
//     This code was generated by a LiveCharts source generator, do not edit.
// </auto-generated>

#pragma warning disable CS0109 // Member does not hide an inherited member; new keyword is not required
{(converter.Length > 0 ? "using LiveChartsCore.SkiaSharpView.TypeConverters;" : string.Empty)}
{target.Headers}

namespace {type.ContainingNamespace};

{GetFormattedAccessibility(type.DeclaredAccessibility)} partial class {declaringType}
{{
{(isXaml
? BindableTemplate(target, template, propertyName, propertyType, sanitizedPropertyType, declaringType, docs, converter)
: template.FullPropertySyntax(target))}
}}";
    }

    public static string GetAvaloniaBaseTypeTemplate(IGrouping<ISymbol?, XamlProperty?> group, FrameworkTemplate template)
    {
        var baseTypeSymbol = group.Key!; // ! not nullable at this point.

        var sb = new StringBuilder();

        foreach (var item in group)
        {
            if (item is not { } value) continue;
            if (value.OnChangeInfo is not { } onChange) continue;

            var displayFormat = new SymbolDisplayFormat(
            typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
            genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);

            var type = value.DeclaringType;
            var propertyType = $"global::{value.Type.ToDisplayString(displayFormat)}";

            var cast = onChange.HasChangeObjectParams ? string.Empty : $"({propertyType})";
            var changeExpression = onChange.HasChangeParams
                ? $@"{onChange.Expression}(({type})c.Sender, {cast}c.OldValue, {cast}c.NewValue)"
                : $@"{onChange.Expression}(({type})c.Sender)";

            _ = sb.AppendLine($"        [\"{value.Name}\"] = c => {changeExpression},");
        }

        var syntaxReference = baseTypeSymbol.DeclaringSyntaxReferences.First();
        var syntaxTree = syntaxReference.SyntaxTree;
        var root = syntaxTree.GetRoot() as CompilationUnitSyntax;
        var usingDirectives = root?.Usings.Select(u => u.ToString());
        var headers = string.Join(@"
", usingDirectives);

        var displayFormat2 = new SymbolDisplayFormat(
            typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameOnly,
            genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);

        var declaringType = baseTypeSymbol.ToDisplayString(displayFormat2);

        return @$"// <auto-generated>
//     This code was generated by a LiveCharts source generator, do not edit.
// </auto-generated>

{headers}

namespace {baseTypeSymbol.ContainingNamespace};

{GetFormattedAccessibility(baseTypeSymbol.DeclaredAccessibility)} partial class {declaringType}
{{
    private readonly System.Collections.Generic.Dictionary<string, System.Action<global::Avalonia.AvaloniaPropertyChangedEventArgs>> _changes = new()
    {{
{sb}
    }};

    private void OnXamlPropertyChanged(global::Avalonia.AvaloniaPropertyChangedEventArgs change)
    {{
        if (_changes.TryGetValue(change.Property.Name, out var action)) action(change);
    }}
}}
";
    }

    private static string BindableTemplate(
        XamlProperty target, FrameworkTemplate template, string propertyName, string propertyType, string sanitizedPropertyType, string declaringType, string docs,
        string converter)
    {
        return @$"
    /// <summary>
    ///    The <see cref=""{propertyName}""/> property definition.
    /// </summary>
    {template.DeclareBindableProperty(propertyName, sanitizedPropertyType)} =
        {template.CreateBindableProperty(propertyName, sanitizedPropertyType, target.Type.IsValueType, declaringType, target.DefaultValueExpression ?? "null", target.OnChangeInfo)}

    {docs}{converter}    public {propertyType} {propertyName}
    {{
        get => ({propertyType})GetValue({propertyName}Property);
        set => SetValue({propertyName}Property, value);
    }}";
    }

    private static string GetFormattedAccessibility(Accessibility accessibility)
    {
        return accessibility switch
        {
            Accessibility.Public => "public",
            Accessibility.Private => "private",
            Accessibility.Internal => "internal",
            Accessibility.Protected => "protected",
            Accessibility.ProtectedAndInternal => "protected internal",
            Accessibility.ProtectedOrInternal => "private protected",
            Accessibility.NotApplicable => "not applicable",
            _ => "unknown"
        };
    }
}
